<?php

namespace App\Http\Controllers\Api\CatchOrder;

use App\Http\Controllers\Api\BaseController;
use App\Repository\CatchConf\DouYinCatchRepository;
use App\Repository\UpThree\MainOrderRepostition;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class PushDouYinOrderController extends BaseController
{
    public $log;

    public function __construct()
    {
        $this->log = new Logger('PushDouYinOrder');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/PushDouYinOrder.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
    }

    //
    public function pushOrder(Request $Request)
    {
        $resDouYinDetail = $getData = file_get_contents('php://input');
        $this->log->info('input' . ($getData));
        $ip = $Request->ip();
        $this->log->info('ip地址:' . $ip);
//        if($ip != '101.126.34.240'){
//            return false;
//        }
        if (!is_array($getData)) {
            $resDouYinDetail = $getData = json_decode($getData, true);
        }
        if (empty($getData['dist_orderno'])) {
            return $this->returnMsg(103, '订单编号为空');
        }
        if (!empty($resDouYinDetail['author_id'])) {
            $whereDis = [
                ['supplier_id', '=', $getData['shop_id']],
                ['dist_code', 'like', '%,' . $resDouYinDetail['author_id'] . ',%'],
            ];
            $disArr = DB::table('supplier')->where($whereDis)->select(['name', 'shop_id'])->first();
            if ($disArr) {
                $getData['douyin_id'] = $disArr->shop_id;
                $getData['douyin_name'] = $disArr->name;
            }
        }


        $orderDatas = $getData;
        if (empty($orderDatas['source'])) {
            $orderDatas['source'] = 1;
        }
        if (!empty($orderDatas['douyin_id'])) {
            $orderDatas['shop_id'] = $orderDatas['douyin_id'];
            $orderDatas['source'] = 6;
        }

        // 农资的不提交
        $models = new DouYinCatchRepository();
        $isTrueOrder = $models->getOne(['dist_orderno' => $orderDatas['dist_orderno']]);
        if (!empty($isTrueOrder->name) && !empty($isTrueOrder->identity) && !empty($isTrueOrder->contact)) {
            return $this->returnMsg(200, '已提交');
        }
//        $this->log->info('orderDatas:' . json_encode($orderDatas));
        $model = new MainOrderRepostition();
        $res = $model->orderSubmit($orderDatas);
        $this->log->info('保存定单结果:' . json_encode($res));
        $orderDatas['status'] = 0;
        if (empty($res['code']) || $res['code'] != 200) {
            $orderDatas['status'] = 60;
        }
        $orderDatas['three_msg'] = !empty($res['msg']) ? $res['msg'] : '订单提交上游失败';

        if ($isTrueOrder) {
            $returnId = $orderDatas['dist_orderno'];
            $isTrue = $models->edit(['dist_orderno' => $orderDatas['dist_orderno']], $orderDatas);
        } else {
            $insertData = $models->add($orderDatas);
        }
        return $this->returnMsg(200, '接收成功');
    }
}
